const {
	Service
} = require("uni-cloud-router");
const {
	httpClientCode
} = require("config");
const {
	getUserInfo
} = require("user-info-admin");
const {
	mainSpaceId
} = require('config-main-space')
const {
	payNotifyUrl
} = require('config-current-space')
const db = uniCloud.database();
const dbCmd = db.command;
const unipay = require('uni-pay')
let isChinaMobile = /^1(3[4-9]|5[012789]|8[23478]|4[478]|7[28]|9[578])\d{8}$/; //移动
let isChinaUnion = /^1(3[0-2]|4[5]|5[56]|6[67]|7[156]|8[56]|9[6])\d{8}$/; //联通
let isChinaTelcom = /^1(3[3])|(8[019])|(5[3])|(7[37])|(9[0139])\d{8}$/; //电信
const wmzAlipaySdk = uniCloud.importObject('wmz-alipay-sdk')
module.exports = class EntitlementService extends(
	Service
) {
	async add(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '新增数据失败',
			data: null
		};
		let uniIdToken = event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)

		let data = event.data.goods
		data.app_id = userInfo.app_id
		data.shop_id = userInfo.shop_id
		data.create_time = new Date().getTime()
		let taskInfoResult = await db.collection('wmz-entitlement').add(data);
		if (taskInfoResult && taskInfoResult.id) {
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '新增数据成功';
			response.data = taskInfoResult.id;
		}

		return response;
	}
	async update(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '修改数据失败',
			data: null
		};
		let uniIdToken = event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)
		let data = event.data.goods
		let id = event.data.id
		let taskInfoResult = await db.collection('wmz-entitlement').doc(id).update(data);
		await db.collection('wmz-applet-info').doc(userInfo.app_id).update({
			callback_count: data.callback_count
		})
		response.code = httpClientCode.SUCCESS.value;
		response.msg = '修改数据成功';
		return response;
	}
	async get(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let id = event.data.id
		let taskInfoResult = await db.collection('wmz-entitlement').doc(id).get();
		taskInfoResult = taskInfoResult.data[0]
		if (taskInfoResult) {
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '查询数据成功';
			response.data = taskInfoResult;
		}
		return response;
	}

	async switchStatus(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let id = event.data._id
		let status = event.data.status
		await db.collection('wmz-entitlement-recharge').doc(id).update({
			recharge_status: status
		});
		response.code = httpClientCode.SUCCESS.value;
		response.msg = '查询数据成功';
		response.data = '';
		return response;
	}
	async getRecharge(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let rechargeDatas = await db.collection('wmz-entitlement-recharge').where(
			dbCmd.or({
				status: '已付款',
				recharge_status: dbCmd.exists(false)
			}, {
				status: '已付款',
				recharge_status: 0
			})).get()
		console.log(rechargeDatas)
		/* let rechargeDatas = await db.collection('wmz-entitlement-recharge').where({
			status: '已付款',
			recharge_status: dbCmd.exists(false)
		}).get(); */
		rechargeDatas = rechargeDatas.data
		response.code = httpClientCode.SUCCESS.value;
		response.msg = '查询数据成功';
		response.data = rechargeDatas;
		return response;
	}

	async buyRefound(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '退款失败',
			data: null
		};
		let id = event.data.id
		let reasu = event.data.reasu
		let order = await db.collection('wmz-entitlement-order').doc(id).get()
		order = order.data[0]
		if (!order) {
			response.msg = '订单错误';
			return response;
		}
		if (order.status === '已退款') {
			response.msg = '订单已退款';
			return response;
		}
		let app_id = order.app_id
		if (order.pay_app_id) {
			app_id = order.pay_app_id
			if (app_id === 'd5eaf8866507d0970006332442bbd569') {
				app_id = 'aefa8b0064ec7d3a00056a253341a167'
			}
		} else {
			if (app_id === 'aefa8b0064ec7d3a00056a253341a167') {
				app_id = 'd5eaf8866507d0970006332442bbd569'
			}
		}
		let res
		if (order.pla === 'app') {
			let unipayIns = unipay.initAlipay({
				mchId: "2088341855742340",
				appId: "2021004123607289",
				alipayPublicKey: "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQE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A0C8RCI5T1w7NzUgUverdPiTneahV7ipR5cgVU5GgYCMXAm8lFy/tha9MTl4FK4d1bkH+YwvUqFZzgWObvhDJ5W/+hX5bBL+RHhac5YS0IG8705mpmpJhaZoct/2VfHVeb3SKPqVFaiHtAP/OLgXvmCzjQXl7Xi/Ya6LQc/UAMNcCgYEAvC3yYSdPhjvKXxr03TD8k1sFQw8EyVCfLfKuBBJEIackOVOpd474s7yAj1W/r+1VbDIah/5tLpTxu60kXTWACT/25XyaTg/BGI2ZKGdiHCTSEDWMRiHnN5GAUd54FC9sH7E8cORQem3dl0ZaBy1fv8PXRZnuhENJ70eIPpp95/kCgYAHX3NZ+zhq6GJPf9sEMtotBj+QZm7MpYELA0AGGqU0YmnBM1B79r2Ru3iXWesS9JQrGHk65sNcRtuGXJG6dHvB/WplLelpTKOGDkT2N/HtKCNVRh7kk4SRqdyUg42Uv89dYSI4P8AqlxL+sxCJPrqSiBxwSC4fUtB2V2jMgoPENwKBgQCQxIA9AbMKP6E2fVR3fiIxcsqfhkRWY7Gx9YquEydbBGy3tYcgNVtbWt9EZeFsBJe8/uauTKvONEOr8E2Tb38qnozgi3ViLHiO3RBApxgxMppwYeGiap6of0VKtm2Gid0SnJU0/FB1f2QD5tmHv/5zVsScBQobsnCEpnFISC4ocQKBgQDAjV0YIr8M92KuwfMlx8FLvAjt1NIX3yLni+5KJSf8hTZkb2CU5sGHt2yzDEL94XP+Z0RmTJDWFTy/bA8wdY44Qtz5jYUQzC8+VTu/RRVJR404F/3vqPYTqZ4vnwT/JR4dnBTVqHPxb468LnZ5FB9XnDuQQC6jbi9Ldjg9bYtsZA==",
				keyType: 'PKCS1'
			});
			res = await unipayIns.refund({
				transactionId: order.trade_no,
				refundFee: order.money * 100
			})
			if (res.code === '10000') {
				res.code = 200
			}
		} else {
			let params = {
				app_id: app_id,
				url: 'alipay.trade.refund',
				biz_content: {
					trade_no: order.trade_no,
					refund_amount: order.money,
					refund_reason: reasu
				}
			}
			res = await wmzAlipaySdk.alipayRequestCommon(params)
		}
		if (res.code === 200) {
			await db.collection('wmz-entitlement-order').doc(id).update({
				status: "已退款",
				refund_time: new Date().getTime()
			})
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '退款成功';
			return response;
		} else {
			response.msg = res.msg;
		}
		return response;
	}

	async rechargeRefound(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '退款失败',
			data: null
		};
		let id = event.data.id
		let reasu = event.data.reasu
		let order = await db.collection('wmz-entitlement-recharge').doc(id).get()
		order = order.data[0]
		if (!order) {
			response.msg = '订单错误';
			return response;
		}
		if (order.status === '已退款') {
			response.msg = '订单已退款';
			return response;
		}
		let app_id = order.app_id
		if (order.pay_app_id) {
			app_id = order.pay_app_id
			if (app_id === 'd5eaf8866507d0970006332442bbd569') {
				app_id = 'aefa8b0064ec7d3a00056a253341a167'
			}
		} else {
			if (app_id === 'aefa8b0064ec7d3a00056a253341a167') {
				app_id = 'd5eaf8866507d0970006332442bbd569'
			}
		}
		let params = {
			app_id: app_id,
			url: 'alipay.trade.refund',
			biz_content: {
				trade_no: order.trade_no,
				refund_amount: order.money,
				refund_reason: reasu
			}
		}

		let res = await wmzAlipaySdk.alipayRequestCommon(params)
		if (res.code === 200) {
			await db.collection('wmz-entitlement-recharge').doc(id).update({
				status: "已退款",
				refund_time: new Date().getTime()
			})
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '退款成功';
			return response;
		} else {
			response.msg = res.msg;
		}
		return response;
	}

	async rechargeMobile(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '充值失败',
			data: null
		};
		let id = event.data.id
		let order = await db.collection('wmz-entitlement-recharge').doc(id).get()
		order = order.data[0]
		if (!order) {
			response.msg = '订单错误';
			return response;
		}
		if (order.ordersn) {
			response.msg = '订单已充值';
			return response;
		}
		if (order.status != '已付款') {
			response.msg = '状态错误';
			return response;
		}
		if (order.recharge_status_text) {
			response.msg = '已充值的订单不能退款';
			return response;
		}
		// 6   全国移动100慢充  92
		// 54   全国电信100慢充（安徽电信暂不支持）  92.5
		// 69   全国联通100慢充  92
		// 397  南方电网200
		let gid = ""
		if (!order.mobile) {
			if (order.goodsType === 'nfdw_recharge') {
				gid = 397
				order.mobile = order.rechargeContent
			}
		} else {
			/* if (isChinaMobile.test(order.mobile)) {
				gid = 6
			} else if (isChinaUnion.test(order.mobile)) {
				gid = 69
			} else if (isChinaTelcom.test(order.mobile)) {
				gid = 54
			} */
			gid = 248
		}
		if (gid) {
			let rechargeSdk = uniCloud.importObject('wmz-recharge-sdk')
			let appId = ""
			let apiKey = ""
			if (order.app_id === "0375964d64e56d470223e6a059620ffe") {
				const mycloud = uniCloud.init({
					provider: 'tencent',
					spaceId: 'tcb-m6g9aep66mev6ffd19470-4faedb'
				})
				rechargeSdk = mycloud.importObject('wmz-recharge-sdk')
			} else {
				let appletInfo = await db.collection('wmz-applet-info').doc(order.app_id).get()
				appletInfo = appletInfo.data[0]
				if (appletInfo) {
					appId = appletInfo.recharge_app_id
					apiKey = appletInfo.api_key
				}
			}
			console.log({
				tradeno: order._id,
				gid: gid,
				mobile: order.mobile,
				total: 1
			})
			let res = await rechargeSdk.submit({
				tradeno: order._id,
				gid: gid,
				mobile: order.mobile,
				total: 1
			}, appId, apiKey)
			console.log(res)
			if (res.status === 200 && res.data.code === 1) {
				await db.collection('wmz-entitlement-recharge').doc(id).update({
					ordersn: res.data.ordersn,
					recharge_status_text: "充值中"
				})
			} else {
				response.msg = res.data.msg;
			}
		} else {
			response.msg = '运营商错误';
			return response;
		}

		return response;
	}

	async getTradeComplainInfos(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let uniIdToken = event.event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)

		let app_id = userInfo.app_id
		/* let appletInfo = await db.collection('wmz-applet-info').doc(app_id).get()
		appletInfo = appletInfo.data[0]
		if (appletInfo) {
			if(appletInfo.alipay.pay_app_id) {
				app_id=appletInfo.alipay.pay_app_id
			}
		} */
		let params = {
			app_id: app_id,
			url: 'alipay.merchant.tradecomplain.batchquery',
			biz_content: {
				status: "MERCHANT_PROCESSING",
				page_size: 10
			}
		}
		let res = await wmzAlipaySdk.alipayRequestCommon(params)
		console.log(res)
		if (res.code === 200) {
			let tradeComplainInfos = []
			if (res.data.tradeComplainInfos) {
				tradeComplainInfos = res.data.tradeComplainInfos
			}
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '查询数据成功';
			response.data = tradeComplainInfos;
		} else {
			response.msg = res.msg;
		}
		return response;
	}


	async sumbitTradeComplainInfos(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let id = event.data.id
		let uniIdToken = event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)
		let app_id = userInfo.app_id
		/* let appletInfo = await db.collection('wmz-applet-info').doc(app_id).get()
		appletInfo = appletInfo.data[0]
		if (appletInfo) {
			if(appletInfo.alipay.pay_app_id) {
				app_id=appletInfo.alipay.pay_app_id
			}
		} */
		let params = {
			app_id: app_id,
			url: 'alipay.merchant.tradecomplain.feedback.submit',
			biz_content: {
				complain_event_id: id,
				feedback_code: "02",
				feedback_content: "您的该笔订单已退款，退款讲原路退回，请注意查收"
			}
		}
		let res = await wmzAlipaySdk.alipayRequestCommon(params)
		if (res.code === 200) {
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '成功';
			response.data = "";
		}
		return response;
	}
	async updatePayAccount(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let data = event.data
		if (data.state === '启用') {
			let appletInfo = await db.collection('wmz-applet-info').where({
				user_name: data.phone
			}).get()
			appletInfo = appletInfo.data[0]
			if (!appletInfo) {
				response.msg = '电话号码不存在';
				return response;
			}
			if (!appletInfo.alipay.auth_app_id) {
				response.msg = '小程序ID不存在';
				return response;
			}
			if (!appletInfo.alipay.profitsharing) {
				response.msg = '未设置分账';
				return response;
			}
			await db.collection('wmz-applet-info').doc(data.app_id).update({
				"alipay.pay_app_id": appletInfo._id,
				"alipay.pay_auth_app_id": appletInfo.alipay.auth_app_id
			})
			let tdb = uniCloud.database({
				provider: 'tencent',
				spaceId: mainSpaceId
			})
			await tdb.collection('wmz-applet-info').doc(data.app_id).update({
				"alipay.pay_app_id": appletInfo._id,
				"alipay.pay_auth_app_id": appletInfo.alipay.auth_app_id
			})
			await db.collection('wmz-pay-account').where({
				state: '启用',
				shop_id: data.shop_id
			}).update({
				state: '禁用'
			})
			await db.collection('wmz-pay-account').doc(data._id).update({
				state: '启用'
			})
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '成功';
			response.data = "";
		} else {
			response.msg = '修改失败,至少启用一条数据';
		}
		return response;
	}

	async updateApplet(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询数据失败',
			data: null
		};
		let data = event.data
		if (data.state === '启用') {
			let tdb = uniCloud.database({
				provider: 'tencent',
				spaceId: mainSpaceId
			})
			let appletInfo = await tdb.collection('wmz-applet-info').where({
				user_name: data.phone
			}).get()
			appletInfo = appletInfo.data[0]
			if (!appletInfo) {
				response.msg = '电话号码不存在';
				return response;
			}
			if (!appletInfo.alipay.auth_app_id) {
				response.msg = '小程序ID不存在';
				return response;
			}
			if (appletInfo.alipay.template_status != 'RELEASE') {
				response.msg = '小程序状态错误';
				return response;
			}

			await db.collection('wmz-applet-info').doc(appletInfo._id).update({
				m_app_id: data.app_id
			})
			await db.collection('wmz-applet-info').doc(data.app_id).update({
				'alipay.pay_app_id': appletInfo._id,
				'alipay.pay_auth_app_id': appletInfo.alipay.auth_app_id
			})

			await db.collection('wmz-entitlement').where({
				app_id: data.app_id
			}).update({
				appid: appletInfo.alipay.auth_app_id
			})

			await db.collection('wmz-entitlement-applet').where({
				state: '启用',
				shop_id: data.shop_id
			}).update({
				state: '禁用'
			})
			await db.collection('wmz-entitlement-applet').doc(data._id).update({
				state: '启用'
			})
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '成功';
			response.data = "";
		} else {
			response.msg = '修改失败,至少启用一条数据';
		}
		return response;
	}

	async balanceRecharge(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '充值失败',
			data: null
		};
		let data = event.data
		let money = data.money
		let uniIdToken = event.event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)
		let unipayIns = unipay.initAlipay({
			mchId: "2088341855742340",
			appId: "2021004123607289",
			alipayPublicKey: "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlFpfs8kw7a9NbPG2TQp4Xo638EUqfRAgVZzQIS8BRQKxFWSQMDE3m7upD3o1mkeNg3LE7htCFZTqYLrkV2TcGdteYb8MAztDICf5YC/rpEr+PQHosvoCCLyaKZThm9bkyfxfkjyHznx/4XlvZhlQkKu9Y6MQLTV4WxbhlYMgOaqBf4HEO63y9me7KOqqbi7gyeAmCpumRG3w26tAthztSxv4rIkbzI4xjxQ2DtVEUMAc7L7ps4S7WjJKh/nkjLwRsUbyAOvkhqAKgUxVjsmJuF75BYUbfO8OKZLlUZXe8Q+nDdE/Ur9kyhS+/dou/5LyrPtbLX5S5IEu2EuxLdkgmQIDAQAB",
			privateKey: "MIIEpAIBAAKCAQEAmQft48bt+Lytdf0cOcxr9jJ1BM2j0yXGOSPV54aKNpws8wPcVXOCgQmXIMIme6kJYPH9FI+t6z4BPpqHsDeVerz4w42S/ivyeu9cZMm5eFZMJHSbFAB2ugWvTS4zNGlSx7grZQofZDMjlrHPm4N84slOnKB6zR8YYHsM44WhxbonOhTG9JJ4QF9abhqb9/A8lV9Swc4tj1Yj3rEahzI8lKTlwQPUExn4Y3Dh6RkxTPmeLS6H3Dx8XpX59s7pbz5jqfGtpRHCh00Mu89I5wI9xX7JV18+4bYZ3hPdMawQ4hEkEL/9LXgQ2HJY9DJ3kKVc3MkMzSX3u7jaFS1nqDyCHwIDAQABAoIBAEN948VZrwrM7BPlldVH+zWR+1oq993gAxSgYW7DP6HmL0zenwZw81XFzQl9l0tiuegv0daHp8Tgi9v4e2+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PYTqZ4vnwT/JR4dnBTVqHPxb468LnZ5FB9XnDuQQC6jbi9Ldjg9bYtsZA==",
			keyType: 'PKCS1'
		});
		let clienIp = this.ctx.context.CLIENTIP
		let notifyUrl = payNotifyUrl + "/notify-bal-recharge/alipay"
		let res = await db.collection('wmz-entitlement-balance-order').add({
			notifyUrl: notifyUrl,
			app_id: userInfo.app_id,
			clienIp: clienIp,
			money: money,
			status: '待付款'
		})
		let outTradeNo = res.id
		let orderInfo = await unipayIns.getOrderInfo({
			subject: "信息技术服务费", // 微信支付时不可填写此项
			body: "信息技术服务费",
			outTradeNo: outTradeNo,
			totalFee: money * 100, // 金额，单位分
			notifyUrl: notifyUrl, // 支付结果通知地址
			spbillCreateIp: clienIp,
			tradeType: "NATIVE"
		})
		if (orderInfo.code === '10000') {
			let qrcodeText = orderInfo.codeUrl
			let qrcodeErrorCorrectLevel = 'M';
			let qrcodeMargin = 2;
			let callFunctionResult = await uniCloud.callFunction({
				name: 'explain-qrcode',
				data: {
					service: 'qrcode',
					action: 'make',
					params: {
						text: qrcodeText,
						ec_level: qrcodeErrorCorrectLevel,
						margin: qrcodeMargin
					}
				}
			});
			if (callFunctionResult.result) {
				response.code = httpClientCode.SUCCESS.value;
				response.msg = '成功';
				response.data = callFunctionResult.result
			}
		}
		return response;
	}
	async balanceGet(event) {
		let response = {
			code: httpClientCode.ERROR.value,
			msg: '查询余额失败',
			data: null
		};
		let uniIdToken = event.event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)
		let appletInfoData = await db.collection('wmz-applet-info').doc(userInfo.app_id).get()
		appletInfoData = appletInfoData.data[0]
		if (appletInfoData && appletInfoData.balance) {
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '成功';
			response.data = appletInfoData.balance
		} else {
			response.code = httpClientCode.SUCCESS.value;
			response.msg = '成功';
			response.data = 0
		}
		return response;
	}
};